package com.ruoyi.emergency.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.emergency.domain.EmTask;
import com.ruoyi.emergency.service.IEmTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2025-07-07
 */
@RestController
@RequestMapping("/emergency/task")
public class EmTaskController extends BaseController
{
    @Autowired
    private IEmTaskService emTaskService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmTask emTask)
    {
        startPage();
        List<EmTask> list = emTaskService.selectEmTaskList(emTask);
        return getDataTable(list);
    }

    /**
     * 查询个人任务列表（只查当前用户任务）
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:my_task:list')")
    @GetMapping("/mylist")
    public TableDataInfo myTaskList(EmTask emTask)
    {
        emTask.setAssigneeId(SecurityUtils.getUserId()); // 只查自己的任务
        startPage();
        List<EmTask> list = emTaskService.selectEmTaskList(emTask);
        return getDataTable(list);
    }

    /**
     * 统计任务总数
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:list')")
    @GetMapping("/count")
    public AjaxResult countAllTask() {
        return AjaxResult.success(emTaskService.countAllTask());
    }

    /**
     * 任务统计接口：总数、今日发布、进行中、已完成、逾期
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:list')")
    @GetMapping("/stats")
    public AjaxResult getTaskStats() {
        int total = emTaskService.countAllTask();
        int today = emTaskService.countTodayTask();
        int doing = emTaskService.countTaskByStatus(1);
        int done = emTaskService.countTaskByStatus(2);
        int overdue = emTaskService.countTaskByStatus(3);
        return AjaxResult.success(new java.util.HashMap<String, Integer>() {{
            put("total", total);
            put("today", today);
            put("doing", doing);
            put("done", done);
            put("overdue", overdue);
        }});
    }

    /**
     * 我的任务统计接口：只统计当前用户的各状态任务数
     * 按 assignee_id=当前用户id，status 分类统计
     */
    @GetMapping("/myStats")
    public AjaxResult getMyTaskStats() {
        Long userId = com.ruoyi.common.utils.SecurityUtils.getUserId();
        int todo = emTaskService.countUserTaskByStatus(userId, 0); // 待处理
        int doing = emTaskService.countUserTaskByStatus(userId, 1); // 进行中
        int done = emTaskService.countUserTaskByStatus(userId, 2); // 已完成
        int overdue = emTaskService.countUserTaskByStatus(userId, 3); // 逾期
        int total = todo + doing + done + overdue;
        java.util.Map<String, Integer> stats = new java.util.HashMap<>();
        stats.put("todo", todo);
        stats.put("doing", doing);
        stats.put("done", done);
        stats.put("overdue", overdue);
        stats.put("total", total);
        return AjaxResult.success(stats);
    }

    /**
     * 任务趋势统计（折线图）
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:report')")
    @GetMapping("/trend")
    public AjaxResult getTaskTrend(@RequestParam String startDate, @RequestParam String endDate) {
        return AjaxResult.success(emTaskService.getTaskTrend(startDate, endDate));
    }

    /**
     * 任务状态分布（饼图）
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:report')")
    @GetMapping("/statusPie")
    public AjaxResult getTaskStatusPie() {
        return AjaxResult.success(emTaskService.getTaskStatusPie());
    }

    /**
     * 责任人任务数排行
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:report')")
    @GetMapping("/assigneeRank")
    public AjaxResult getAssigneeRank() {
        return AjaxResult.success(emTaskService.getAssigneeRank());
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmTask emTask)
    {
        List<EmTask> list = emTaskService.selectEmTaskList(emTask);
        ExcelUtil<EmTask> util = new ExcelUtil<EmTask>(EmTask.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息 @PreAuthorize("@ss.hasPermi('emergency:task:query')")
     */

    @GetMapping(value = "/{id:\\d+}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(emTaskService.selectEmTaskById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmTask emTask)
    {
        return toAjax(emTaskService.insertEmTask(emTask));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmTask emTask)
    {
        return toAjax(emTaskService.updateEmTask(emTask));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('emergency:task:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emTaskService.deleteEmTaskByIds(ids));
    }
}
